#include <iostream>
#include <string>
#include <set>
#include <stdio.h>

class Command
{
public:
    Command()
    {
        _white_list.insert("ls");
        _white_list.insert("pwd");
        _white_list.insert("ls -l");
        _white_list.insert("ll");
        _white_list.insert("touch");
        _white_list.insert("who");
        _white_list.insert("whoami");
    }

    bool Check(std::string command)
    {
        auto iter = _white_list.find(command);
        if (iter == _white_list.end()) // 说明没找到
        {
            return false;
        }
        return true;
    }

    std::string CommandHandler(std::string command)
    {
        if (!Check(command))
        {
            return std::string(command + " 不支持");
        }
       
        FILE *fp = ::popen(command.c_str(), "r");

        if (nullptr == fp)
        {
            return std::string("Failed");
        }
        char buffer[1024];
        std::string result;
        while (true)
        {
            char *ret = ::fgets(buffer, sizeof(buffer), fp);
            if (!ret)
                break;
            result += ret;
        }
        pclose(fp);
        return result.empty() ? std::string("Done") : result;
            //result += '0';
        
    }

    ~Command()
    {
    }

private:
    std::set<std::string> _white_list;
};